//axios二次封装
import axios from "axios";
import useUserStore from "@/store/modules/user";

//创建axios实例
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
});
//请求拦截器
request.interceptors.request.use(
  (config) => {
    let userStore = useUserStore();
    if (userStore.token) {
      config.headers.token = userStore.token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
//响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    //@ts-ignore
    let msg = "";
    if (error.response.status === 404) {
      msg = "请求资源不存在";
    } else if (error.response.status === 500) {
      msg = "服务器错误";
    } else {
      msg = error.message;
    }
    return Promise.reject(error);
  }
);
export default request;
